﻿using System;
using System.Configuration;

namespace Higgs.Web.AtomPub.Configurations
{
    public class MediaCollectionConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("id", IsRequired = true)]
        public string Id
        {
            get { return (string)this["id"]; }
            set { this["id"] = value; }
        }

        [ConfigurationProperty("guid", IsRequired = false)]
        public string Guid
        {
            get { return (string)this["guid"]; }
            set { this["guid"] = value; }
        }

        [ConfigurationProperty("title", IsRequired = false, DefaultValue = "Blog Entry")]
        public string Title
        {
            get { return (string)this["title"]; }
            set { this["title"] = value; }
        }

        [ConfigurationProperty("routePath", IsRequired = false, DefaultValue = "Media/{id}")]
        public string RoutePath
        {
            get 
            { 
                var routePath = (string) this["routePath"];
                
                return AtomPubConfigSection.Current.ControllerPath + routePath; 
            }
            set { this["routePath"] = value; }
        }

        [ConfigurationProperty("staticPath", IsRequired = false, DefaultValue = "Blog")]
        public string StaticPath
        {
            get 
            { 
                return (string) this["staticPath"];
            }
            set { this["staticPath"] = value; }
        }

        public string Path
        {
            get { return RoutePath.RemoveUrlParameter(); }
        }

        [ConfigurationProperty("acceptTypes", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(AcceptTypeElementCollection))]
        public AcceptTypeElementCollection AcceptTypes
        {
            get { return (AcceptTypeElementCollection)base["acceptTypes"]; }
        }

        [ConfigurationProperty("storedPath", IsRequired = false, DefaultValue = "~/Blog/Media")]
        public string StoredPath
        {
            get { return (string)this["storedPath"]; }
            set { this["storedPath"] = value; }
        }

        [ConfigurationProperty("enableUpdate", IsRequired = false, DefaultValue = true)]
        public bool EnableUpdate
        {
            get { return (bool)this["enableUpdate"]; }
            set { this["enableUpdate"] = value; }
        }

        public AcceptTypeElement GetAcceptTypeByExtension(string extension)
        {
            for (var i = 0; i < AcceptTypes.Count; i++)
            {
                var at = AcceptTypes[i];
                if (!at.Extension.Equals(extension, StringComparison.CurrentCultureIgnoreCase)) continue;

                return at;
            }

            return null;
        }
    }
}
